﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;

namespace DataProvider
{
    public static class RsUtils
    {
        private static Dictionary<String, Assembly> assemblyPool = new Dictionary<string, Assembly>();
        private static String excutePath = null;
        /////////////////////////////////////////////////////////////////////////////////////

        public static String GetResourceString(String resourceName)
        {
            return GetResourceString(Assembly.GetExecutingAssembly(), resourceName);
        }

        public static String GetResourceString(Assembly assembly, String resourceName)
        {
            String resourceString = "";
            try
            {
                using (Stream stream = assembly.GetManifestResourceStream(resourceName))
                {
                    using (StreamReader reader = new StreamReader(stream, Encoding.Default))
                    {
                        resourceString = reader.ReadToEnd();
                    }
                }
            }
            catch (Exception e)
            {
                DataLayerLog.LogError(e.Message);
            }
            return resourceString;
        }
        public static Stream GetResourceStream(String assemblyName, String resourceName)
        {
            try
            {
                Assembly assembly = LoadAssemblyFromFile(assemblyName);
                return assembly.GetManifestResourceStream(resourceName);
            }
            catch (Exception e)
            {
                DataLayerLog.LogError(e.Message);
            }
            return null;
        }

        public static String GetResourceString(String assemblyName, String resourceName)
        {
            Assembly assembly = LoadAssemblyFromFile(assemblyName);
            return GetResourceString(assembly, resourceName);
        }

        public static String[] GetResourceManifest(String assemblyName)
        {
            Assembly assembly = LoadAssemblyFromFile(assemblyName);
            return assembly.GetManifestResourceNames();
        }

        public static Assembly LoadAssemblyFromFile(String assemblyName)
        {
            if (assemblyPool.ContainsKey(assemblyName))
                return assemblyPool[assemblyName];

            if (String.IsNullOrEmpty(excutePath))
            {
                String filePath = Assembly.GetExecutingAssembly().Location;
                int lastPostion = filePath.LastIndexOf("\\");
                excutePath = filePath.Substring(0, lastPostion).TrimEnd('\\') + "\\";
            }
            String dllFile = excutePath + assemblyName;
            if (File.Exists(dllFile))
            {
                Assembly assembly = Assembly.LoadFrom(dllFile);
                assemblyPool[assemblyName] = assembly;
                return assembly;
            }
            else
            {
                DataLayerLog.LogInfo("Can not load:" + dllFile);
            }
            return null;
        }
 
    }
}
